package com.isorf.controller;


import com.github.pagehelper.PageInfo;
import com.isorf.pojo.brand;
import com.isorf.pojo.color;
import com.isorf.pojo.links;
import com.isorf.service.UploadService;
import com.isorf.service.brandservice;
import com.isorf.util.responseresult;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
public class brandcontroller
{
    @Autowired
    private brandservice brandservice; // Variable name should match the field name
    @Autowired
    private UploadService uploadservice;
    @Value("${file.upload.path}")     // 从application.yml文件中获取图片上传的路径
    private String path;

    @PostMapping("/updatebrand")
    public responseresult<Boolean> updatebrand(@RequestBody brand brand) {
        if (brand == null || brand.getBrandid() == null) {
            return responseresult.failure(responseresult.FAILED, "更新失败，缺少必要参数");
        }
        Boolean result = brandservice.updatebrand(brand);
        if (result) {
            return responseresult.success(responseresult.SUCCESS, "更新成功", result);
        } else {
            return responseresult.failure(responseresult.FAILED, "更新失败");
        }
    }

    @PostMapping("/addbrand")
    public responseresult<Boolean> addbrand(@RequestBody brand brand) {
        Boolean result = brandservice.addbrand(brand);
        if (result) {
            return responseresult.success(responseresult.SUCCESS, "add successful", result);
        } else {
            return responseresult.failure(responseresult.FAILED, "add failed");
        }

    }

    @PostMapping("/uploadIImage")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(
                    name = "flimage",
                    value = "上传的图片文件",
                    required = true,
                    paramType = "formData",
                    dataType = "MultipartFile",
                    dataTypeClass = MultipartFile.class,
                    defaultValue = "")})
    public responseresult uploadImage(@RequestPart MultipartFile flimage) {
        String flag = uploadservice.uploadImage(path, flimage);
        if (flag!=null) {
            return responseresult.success(responseresult.SUCCESS, "上传成功", flag);
        }else{
            return responseresult.failure(responseresult.FAILED, "上传失败");
        }
    }

    @PostMapping("/pageLList")
    public responseresult<PageInfo<brand>> pageList(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "5") int pageSize) {
        try {
            PageInfo<brand> result = brandservice.pageList(pageNum, pageSize);

            // 修复逻辑错误：先检查result是否为null，再检查列表
            if (result == null) {
                return responseresult.failure(responseresult.FAILED, "查询失败，返回结果为空");
            }

            if (result.getList() == null || result.getList().isEmpty()) {
                // 返回空列表，但不认为是错误
                return responseresult.success(responseresult.SUCCESS, "查询成功，暂无数据", result);
            }

            return responseresult.success(responseresult.SUCCESS, "查询成功", result);
        } catch (Exception e) {
            // 添加异常处理
            e.printStackTrace();
            return responseresult.failure(responseresult.FAILED, "查询失败：" + e.getMessage());
        }
    }





    @PostMapping("/batchDDelete")
    public responseresult<Boolean> batchDelete(@RequestBody List<Integer> ids)
    {
        // 检查参数
        if (ids == null || ids.isEmpty()) {
            return responseresult.failure(responseresult.FAILED, "删除失败，缺少必要参数");
        }

        // 调用service层的批量删除方法
        Boolean result = brandservice.batchDelete(ids);
        if (result) {
            return responseresult.success(responseresult.SUCCESS, "批量删除成功", result);
        } else {
            return responseresult.failure(responseresult.FAILED, "批量删除失败");
        }


    }

    @GetMapping("/deletebrand")
    public responseresult<Boolean> deletebrand(@RequestBody brand brand) {
        // 检查link对象和id是否为空
        if (brand == null || brand.getBrandid() == null) {
            return responseresult.failure(responseresult.FAILED, "删除失败，缺少必要参数");
        }

        // 调用service层的删除方法，应该是一个删除方法而不是addlink
        Boolean result = brandservice.deletebrand(brand.getBrandid());
        if (result) {
            return responseresult.success(responseresult.SUCCESS, "删除成功", result);
        } else {
            return responseresult.failure(responseresult.FAILED, "删除失败");
        }
    }


    @GetMapping("/getallbrand")
    public responseresult<List<brand>> getallbrand() {
        List<brand> brandList = brandservice.getallbrand();
        if (brandList == null) {
            return responseresult.failure(508, "没有学生信息");
        }else {
            return responseresult.success(200, "查询成功", brandList);

        }
    }


}
